from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa

# 生成 RSA 私钥
private_key = rsa.generate_private_key(
    public_exponent=65537,
    key_size=2048,  # 也可以用 4096
)

# 将私钥写入 PEM 文件
with open("config/private.pem", "wb") as f:
    f.write(private_key.private_bytes(
        encoding=serialization.Encoding.PEM,
        format=serialization.PrivateFormat.PKCS8,  # 常用格式
        encryption_algorithm=serialization.NoEncryption()  # 可加密
    ))

# 生成对应的公钥
public_key = private_key.public_key()

# 将公钥写入 PEM 文件
with open("config/public.pem", "wb") as f:
    f.write(public_key.public_bytes(
        encoding=serialization.Encoding.PEM,
        format=serialization.PublicFormat.SubjectPublicKeyInfo
    ))

print("✅ RSA 公私钥已生成：private.pem, public.pem")
